import {observable, action,computed} from 'mobx';
import { Base64 } from 'js-base64';
import { Tools,Toast,Modal,Bubble } from 'weaver-mobile';
import { Reply,Comment } from 'weaver-mobile-page';
import * as goalBase from '../../api/goal/goalBase'
const getLabel = Tools.getLabel;
const alert = Modal.alert;
export default class GoalCommentStore {

    @observable commentList = [];
    @computed
    get replyStore() {
        return new Reply.Store({ getAttachList: this.getAttachList, getAtConfig: this.getAtConfig }); // 没有 + 和 @ 功能，可以直接 new Reply.Store();
    }
    @observable 
    viewdate = ""//浏览时间

    @action
    changeViewdate = (viewdate) => {
        this.viewdate = viewdate
    }
    @observable goalId = "";  //当前目标id
    @observable pageSize = 5; //分页大小
    @observable pageNum = 0;  //当前页数
    @observable canDelete = false; //能否删除
    @observable replyFocus = false;
    @observable replyid = ""; //回复评论的id
    @observable mainId = ""; //主评论的id
    @observable toUserId = "";//被回复的用户的id
    @observable allCommentList = [];//所有的评论数据
    @observable commentStore = new Comment.Store({
      getComment: this.getComment,
      operate:this.operate,
      getChild: null
    });
    setGoalId = (goalId) => {
      this.goalId = goalId;
    }

    @observable
    replyAttachDatas = {}

    @action
    changeReplyAttachDatas = (replyAttachDatas) => {
        this.replyAttachDatas = replyAttachDatas
    }

    @observable
    category = ""

    @action
    changeCategory = (category) => {
        this.category = category
    }

    @computed
    get getAttachList() {
        return () => {
            return new Promise((resolve) => {
                resolve({
                    attachList: [
                        {
                            key: 'doc',
                            type: 'browser',
                            path: '/goalHome/goalDetail/',
                            config: {
                                type: 37,
                                isSingle: false,
                            },
                            iconType: 'document-big',
                            iconBgColor: '#F3B04C',
                            text: getLabel(58, '文档'),
                        },
                        { // 浏览按钮类型组件type为 browser
                            key: 'flow', // 字符串
                            type: 'browser',
                            path: '/goalHome/goalDetail/',
                            config: {
                                type: 152,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: 'flow',
                            iconBgColor: '#61A8F5',
                            text: getLabel(18015, '流程'),
                        },
                        { // 浏览按钮类型组件type为 browser
                            key: 'customer', // 字符串
                            type: 'browser',
                            path: '/goalHome/goalDetail/',
                            config: {
                                type: 7,
                                isSingle: false,
                                hasAdvanceSerach: true,
                            },
                            iconType: 'customer',
                            iconBgColor: '#61A8F5',
                            text: getLabel(136, '客户'),
                        },
                        {
                            key: 'project',
                            type: 'browser',
                            path: '/goalHome/goalDetail/',
                            config: {
                                type: 8,
                                isSingle: false,
                            },
                            iconType: 'relevant-project',
                            iconBgColor: '#F3B04C',
                            text: getLabel(101, '项目'),
                        },
                        { // 上传类型组件type为upload
                            key: 'file',
                            type: 'upload',
                            path: '/goalHome/goalDetail/',
                            config: {
                                multiSelection: true,
                                uploadUrl: "/api/doc/upload/uploadFile2Doc?category=" + this.category
                            },
                            iconType: 'file',
                            iconBgColor: '#F3B04C',
                            text: getLabel('156', '附件'),
                        },
                    ],
                });
            })
        }
    }
    
    @computed
    get getAtConfig() {
        return () => {
            return new Promise((resolve) => {
                resolve({
                    atConfig: {
                        key: 'at',
                        path: '/goalHome/goalDetail/',
                        config: {
                            type: 16,
                            isSingle: false,
                        },
                    },
                });
            })
        }
    }

    @action("添加反馈")
    addGoalFeedBack = (params) =>{
      params.mainId = this.getCommentMainId(params.replyid);
      return goalBase.addGoalFeedBack(params).then(action(res => {
        if(res.status){
          Bubble.success(getLabel(390839,'反馈成功') , 1);
          this.commentList = [];
          this.replyid = "";   
          if(res.mobileComment){
            this.commentStore.doOperateReply({
              replyId:params.mainId,
              parentId:null,
              reply:res.mobileComment
            })
          }else{
            this.commentStore.getDatas();
          }
        }else{
          Bubble.error(res.msg || getLabel('25009','失败'), 1);
        }
        this.doClearReply();
      }));
    }

    @action("删除反馈")
    delGoalFbInfo  = (params) =>{
      goalBase.delGoalFbInfo(params).then(action(res => {
        if(res.api_status){
          Bubble.success(getLabel(20461,'删除成功') , 1);
        }else{
          Bubble.error(res.msg || getLabel(25009,'失败'), 1);
        }
      }));
    }
  
    // // 实际接口调用的方法
    @action("获取评论")
    getComment = () => {
      let params = {}
      params.goalId = this.goalId;
      params.pageSize = this.pageSize;
      params.pageNum = this.pageNum;
      params.viewdate = this.viewdate
      if(this.commentList && this.commentList.length > 0){
        let key = this.commentList.length - 1;
        params.lastId = this.commentList[key].replyId;
      }
      return Tools.callApi({
          url: '/api/workrelate/goal/goalListComment',
          method: 'POST',
          params,
        }).then(action(res => {
            if(res.status){
              this.commentList = res.replyList;
              for(let i = 0;i < res.replyList.length; i ++){
                this.allCommentList.push(res.replyList[i])
              }
              return res
            }
        }))
    }

    @action 
    operate = (item) => {
      const {type, data, parent} = item;
      if(type.type == 'blog-comment'){
        this.replyFocus = true;
        this.replyid = data.replyId;
        // this.mainId = data.replyMainId ? data.replyMainId :  data.replyId;  //没有主评论，则默认当前评论为主评论
        // this.toUserId = data.creater.id;
      }else if(type.type == 'delete'){
        alert(getLabel(388758, '确认要删除吗？'),'', [
          { text:  getLabel(31129, '取消') },
          { text: getLabel(33703, '确认'),
            onPress: () => {
              let params = {
                goalId:this.goalId,
                fbid:item.data.replyId
              }

              return goalBase.delGoalFbInfo(params).then(action(res => {
                this.commentStore.doOperateReply({
                  replyId: data.replyId,
                  parentId: parent ? parent.replyId : '',
                  reply:null,
                })
              }));

            }},
        ])
        // this.delGoalFbInfo(params)
      }

      // let func = null;
      // if (params.type === 'delete') {
      //   if (params.data.replyId === 141) {
      //     func = new Promise((resolve) => {
      //       resolve({
      //         replyId: 141,
      //         reply: null,
      //       });
      //     });
      //   }
      //   if (params.data.replyId === 190) {
      //     func = new Promise((resolve) => {
      //       resolve({
      //         replyId: 190,
      //         parentId: 141,
      //         reply: null,
      //       });
      //     });
      //   }
      //   if (params.data.replyId === 191) {
      //     func = new Promise((resolve) => {
      //       resolve({
      //         replyId: 191,
      //         parentId: 141,
      //         reply: null,
      //       });
      //     });
      //   }
      // }
      // return func;
    }

    /**
     * 获取主评论的id
     */
    getCommentMainId = (id) =>{
      let mainId = "";
      let len = this.allCommentList.length;
      for(let i = 0;i < len;i ++){
        if(id == this.allCommentList[i].replyId){
          mainId = this.allCommentList[i].replyId;
        }else{
          let childList = this.allCommentList[i].childReply;
          if(childList){
            for(let j = 0;j < childList.length;j ++){
              if(id == childList[j].replyId){
                mainId = childList[j].replyMainId;
              }
            }
          }
        }
      }
      return mainId;
    }

    /**
     * 清空回复区
     */
    doClearReply = () =>{
      this.replyStore.doClear();
      this.replyFocus = false;
    }
    
    @action
    goalListComment = (params) => {
      goalBase.goalListComment(params).then(action(res => {
          if(res.status){
              // message.error(res.msg || getLabel('25009','失败'));
              // this.cateList = this.setCateList(res.cateList);
              return res.data;
          }else{
              // message.error(res.msg || getLabel('25009','失败'));
          }
      }));
    }

    @action
    changeCommentList = (commentList) =>{
      this.commentList = commentList;
    }
    
    @action
    changeAllCommentList = (allCommentList) =>{
      this.allCommentList = allCommentList;
    }
}